const Mock = require("mockjs");
const util = require("./util"); //自定义工具模块

module.exports = app => {
  //监听http请求
  app.post("/manager/checkLogin", (req, res) => {
    let json = util.getJsonFile("./login.json");
    res.json(Mock.mock(json));
  });
  app.post("/menuRole/loadAllIdsByUser", (req, res) => {
    let json = util.getJsonFile("./permission_code.json");
    res.json(Mock.mock(json));
  });
  app.post("/merchant/convenient-info/loadAllByQuery", (req, res) => {
    let json = util.getJsonFile("./convenient_info.json");
    res.json(Mock.mock(json));
  });
  app.get("/merchant/convenient-info/loadOne", (req, res) => {
    let json = util.getJsonFile("./convenient_info.json");
    const id = req.query.convenientInfoId;
    const arr = json.object.records;
    const record = arr.find(item => item.id === +id);
    const data = { code: "200", msg: "", object: record };

    res.json(Mock.mock(data));
  });
  app.post("/manager/loadAllByQuery", (req, res) => {
    let json = util.getJsonFile("./user.json");
    res.json(Mock.mock(json));
  });
  app.get("/staff/department/loadAllBySelect", (req, res) => {
    let json = util.getJsonFile("./dept.json");
    res.json(Mock.mock(json));
  });
  app.get("/staff/job/loadSelectByDepartment", (req, res) => {
    let json = util.getJsonFile("./job.json");
    res.json(Mock.mock(json));
  });
  app.post("/inforelease/community-announcement/loadAllByQuery", (req, res) => {
    let json = util.getJsonFile("./info.json");
    res.json(Mock.mock(json));
  });
  app.get("/inforelease/community-announcement/loadOne", (req, res) => {
    let json = util.getJsonFile("./info.json");
    const id = req.query.communityAnnouncementId;
    const arr = json.object.records;
    const record = arr.find(item => item.id === +id);
    const data = { code: "200", msg: "", object: record };

    res.json(Mock.mock(data));
  });
  app.get("/manager/loadOne", (req, res) => {
    let json = util.getJsonFile("./user_info.json");
    const id = req.query.internalUserId;
    const arr = json.rows;
    const record = arr.find(item => item.id === +id);
    const data = { code: "200", msg: "", object: record };

    res.json(Mock.mock(data));
  });
  app.post("/business/apartment-info/loadAllByQuery", (req, res) => {
    let json = util.getJsonFile("./apartment.json");
    res.json(Mock.mock(json));
  });
  app.post("/business/apartment-details/loadAllByQuery", (req, res) => {
    let json = util.getJsonFile("./apartment_detail.json");
    res.json(Mock.mock(json));
  });
  app.get("/business/apartment-info/charge/loadAllByMaster", (req, res) => {
    let json = util.getJsonFile("./charge.json");
    res.json(Mock.mock(json));
  });
  app.get("/dictionary/loadAllByFieldName", (req, res) => {
    let json = util.getJsonFile("./dictionary.json");
    const fieldName = req.query.fieldName;
    const record = json[fieldName];
    const data = { code: "200", msg: "", rows: record };

    res.json(Mock.mock(data));
  });
  app.get("/staff/job/loadAllInfoByProperty", (req, res) => {
    let json = util.getJsonFile("./role.json");
    res.json(Mock.mock(json));
  });
  app.post("/menuRole/loadAllByJob", (req, res) => {
    let json = util.getJsonFile("./permission.json");
    res.json(Mock.mock(json));
  });
  app.post("/admin/loadAllByQuery", (req, res) => {
    let json = util.getJsonFile("./admin.json");
    res.json(Mock.mock(json));
  });
  app.get("/address/loadAllProvinces", (req, res) => {
    let json = util.getJsonFile("./address.json");
    const record = json.province;
    const data = { code: "200", msg: "", rows: record };

    res.json(Mock.mock(data));
  });
  app.get("/address/loadAllCityByProvinces", (req, res) => {
    let json = util.getJsonFile("./address.json");
    const record = json.city;
    const data = { code: "200", msg: "", rows: record };

    res.json(Mock.mock(data));
  });
  app.get("/address/loadAllAreasByCity", (req, res) => {
    let json = util.getJsonFile("./address.json");
    const record = json.area;
    const data = { code: "200", msg: "", rows: record };

    res.json(Mock.mock(data));
  });
  app.get("/address/loadAllStreetByAreas", (req, res) => {
    let json = util.getJsonFile("./address.json");
    const record = json.street;
    const data = { code: "200", msg: "", rows: record };

    res.json(Mock.mock(data));
  });
  app.get("/manager/sendCheckCode", (req, res) => {
    res.json(Mock.mock({ msg: "获取成功", code: "200" }));
  });
};
